﻿using Grpc.Core;
using Microsoft.AspNetCore.Mvc;
using MyWeb.Model;

namespace MyWeb.Api.Controllers
{
    /// <summary>
    /// 控制器基类
    /// </summary>
    public class BaseController : Controller
    {
        /// <summary>
        /// 成功
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        [NonAction]
        public ApiResult Success<T>(T data, string msg = "成功")
        {
            return new ApiResult()
            {
                Success = true,
                Msg = msg,
                Response = data,
            };
        }


        /// <summary>
        /// 成功
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        [NonAction]
        public ApiResult Success(string msg = "成功")
        {
            return new ApiResult()
            {
                Success = true,
                Msg = msg,
                Response = null,
            };
        }
        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        [NonAction]
        public ApiResult Failed(string msg)
        {
            return new ApiResult()
            {
                Success = false,
                Status = 500,
                Msg = msg,
                Response = null,
            };
        }
        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="msg">消息</param>
        /// <param name="status">状态码</param>
        /// <returns></returns>
        [NonAction]
        public ApiResult Failed(string msg = "失败", int status = 500)
        {
            return new ApiResult()
            {
                Success = false,
                Status = status,
                Msg = msg,
                Response = null,
            };
        }
        /// <summary>
        /// 失败
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data">响应数据</param>
        /// <param name="msg">消息</param>
        /// <param name="status">状态码</param>
        /// <returns></returns>
        [NonAction]
        public ApiResult Failed<T>(T? data, string msg = "失败", int status = 500)
        {
            return new ApiResult()
            {
                Success = false,
                Status = status,
                Msg = msg,
                Response = data,
            };
        }
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="page"></param>
        /// <param name="dataCount"></param>
        /// <param name="pageSize"></param>
        /// <param name="data"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        [NonAction]
        public ApiResult SuccessPage<T>(int page, int dataCount, int pageSize, List<T> data, string msg = "获取成功")
        {

            return new ApiResult()
            {
                Success = true,
                Msg = msg,
                Response = new PageModel<T>(page, dataCount, pageSize, data)
            };
        }
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="pageModel"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        [NonAction]
        public ApiResult SuccessPage<T>(PageModel<T> pageModel, string msg = "获取成功")
        {

            return new ApiResult()
            {
                Success = true,
                Msg = msg,
                Response = pageModel
            };
        }
    }
}
